package com.goldgov.statistics.service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.goldgov.information.service.ArticleService;

@Component
@EnableScheduling
public class StatisticsBQSchedule {

	protected final Log logger = LogFactory.getLog(this.getClass());
	
	public static List<String[]> bqList=new ArrayList<String[]>();
	
	@Autowired
	private ArticleService articleService;
	
	//目前效率还可以,请忽略此方法
//	@Lazy(false)
//	@Scheduled(cron = "${task.cron.autoStatisticsBQ}")
	@Deprecated
	public void collectPolling() {
		LocalDateTime localDateTime =LocalDateTime.now();
		logger.info("当前时间为:" + localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		List<String[]> bqListTemp=new ArrayList<String[]>();
		Map<String,Integer> aMap=new HashMap<>();
		Map<String,Integer> uMap=new HashMap<>();
		List<String> articleBQ=articleService.statisticsBQByArticle();
		for(String s:articleBQ){
			for(String ss:s.split(",")){
				aMap.put(ss.trim(), aMap.containsKey(ss.trim())?aMap.get(ss.trim())+1:1);
			}
		}
		logger.info("文章标签为:"+articleBQ.size());
		List<String> userBQ=articleService.statisticsBQByUser();
		for(String s:userBQ){
			for(String ss:s.split(",")){
				uMap.put(ss.trim(), uMap.containsKey(ss.trim())?uMap.get(ss.trim())+1:1);
			}
		}
		logger.info("用户标签为:"+userBQ.size());
		//删除空格
		aMap.remove("");
		//循环存入集合
		String[] as = new String[aMap.size()];
		aMap.keySet().toArray(as);
		String[] us = new String[uMap.size()];
		uMap.keySet().toArray(us);
		for(int i=0;i<as.length;i++){
			b:for(int j=0;j<us.length;j++){
				if(as[i].equals(us[j])){
					bqListTemp.add(new String[]{aMap.get(as[i])+"",uMap.get(us[j])+"",as[i]});
					break b;
				}
				if(j==us.length-1){
					bqListTemp.add(new String[]{aMap.get(as[i])+"","0",as[i]});
				}
			}
		}
		bqList.clear();
		bqList.addAll(bqListTemp);
		logger.info("========存入成功");
	}
}
